package com.qfang.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author xsq
 * 
 */
public class ArrayUtil {
	public static boolean isEmpty(Object[] objs) {
		if (null == objs || objs.length < 1)
			return true;
		return false;
	}

	public static boolean isEmpty(Collection c) {
		if (null == c || c.size() < 1)
			return true;
		return false;
	}

	/**
	 * 移除 集合中的一个元素字段名为name的字段的值为value
	 * 
	 * @param c
	 * @param name
	 * @param value
	 */
	public static Object remove(Collection c, String fieldName,
			String fieldValue) {
		if (isEmpty(c)) {
			return null;
		}
		Iterator iter = c.iterator();
		Object itemObj = null;
		while (iter.hasNext()) {
			itemObj = iter.next();
			if (fieldValue.equals(ClassUtil.getFieldValue(itemObj, fieldName))) {
				iter.remove();
				return itemObj;
			}
		}
		return null;
	}

	public static Collection removeAll(Collection list, Collection removeList,
			String fieldName) {
		if (isEmpty(list) || isEmpty(removeList)) {
			return list;
		}

		Iterator iter = removeList.iterator();
		Object itemObj = null;
		Object value = null;
		while (iter.hasNext()) {
			itemObj = iter.next();
			value = ClassUtil.getFieldValue(itemObj, fieldName);
			remove(list, fieldName, value + "");
			if (isEmpty(list))
				break;
		}
		return list;
	}

	public static void main(String[] args) {
            List list=new ArrayList();
	        list.add(new Stu("1"));
	        list.add(new Stu("11"));
	        list.add(new Stu("12"));
	        list.add(new Stu("13"));
	        list.add(new Stu("14"));
	        
	        List rList=new ArrayList();
	        rList.addAll(list);
	        
	        list.remove(0);
	        System.out.println(removeAll(rList,list,"id"));
	}
	
	public static class Stu{
		private String id;
		private String name;
		
		
		public Stu(String id) {
			super();
			this.id = id;
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		public String toString()
		{
			return this.id;
		}
		
		
	}
}
